<?php
/**
 * Created by 王跃达.
 * User: 小马出行
 * Date: 2017/8/24
 * Time: 16:17
 */

namespace app\api\model;


use my\Define;
use think\Db;
use think\Model;

class OrderModel extends Model
{


	public function __construct()
	{
		parent::__construct();

	}

	/**
	 * 客户端下单前的城市搜索按大写字母排序
	 * @param string 
	 * @param int 
	 * @return array//成功时返回
	 */
	public function citySearch(){
		$list = Db::name('zone')->order('first_name asc,zone_id asc')->group('first_name')->select();
	    $res = array();
		foreach ($list as $key => $value) {
			$res[$key] = array('number'=>$value['first_name'],'citys'=>Db::name('zone')->field('first_name,region_id,zone_name as city_name')->where('is_use',1)->where('first_name',$value['first_name'])->order('zone_id asc')->select());
		}
		return $res;
	}

	/**
	 * 用户充值记录
	 * @param int $recharge_id admin_rechargeID值
	 * @param int $type 1支付宝2微信支付
	 * @return array
	 */
	public function createRecharge($uid, $money,$return_money,$type,$recharge_id){
		$data['uid'] = $uid;
		$data['money'] = $money;
		$data['type'] = $type;
		$data['out_trade_no'] = 'RC'.date("YmdHis", time()) . rand(10000, 99999);
		$data['time'] = time();
		$data['status'] = 0;//返回给客户端的订单信息0是无效1有效
        $data['money_statement'] = 1;//1充值2消费
        $data['return_money'] = $return_money;//1充值赠送的金额
        $data['admin_recharge'] = $recharge_id;//后台充值送余额的ID
		$res = Db::name('user_recharge')->insertGetId($data);
		if($res){
			return $data;
		}

	}
	/**
	 * 创建一个订单
	 * @param string 
	 * @param int 
	 * @return string//成功时返回
	 */
	public function createOrder($data)
	{
		return Db::name('order_common')->insertGetId($data);
	}
	/**
	 * 订单日志
	 * @param string 
	 * @param int 
	 * @return string//成功时返回
	 */
	public function createOrderLog($data){
		return Db::name('order_log')->insertGetId($data);
	}

	public function findUnfinishedOrder($uid)
	{
		$this->cancelOrder($uid);
		$map['uid'] = $uid;
		$map['order_status'] = array("in", array(0, 20, 30, 40, 45));
		return Db::name('order_common')->field("order_sn,order_status")->where($map)->find();

	}

	public function cancelOrder($uid){
		$where["uid"] = $uid;
		$where['order_status'] = 0;
		$where['order_time'] = array("elt", time() - Define::CANCEL_TIME);
		$statusEq0 = Db::name('order_common')->field("common_id")->where($where)->find();
		if (!empty($statusEq0)) {
			Db::name('order_common')->update(['order_status' => '10', 'common_id' => $statusEq0["common_id"], 'cancel_time' => time()]);
		}
	}

	public function getOrderByOrderSn($uid,$common_id,$field)
	{
		//$this->cancelOrder($uid);
		$where['common_id']=$common_id;
		return Db::name('order_common')->field($field)->where($where)->find();
	}

	public function getOrderListByUid($uid,$field)
	{
		$this->cancelOrder($uid);
		$where["uid"] = $uid;
		$where['order_time']=array("gt",time()-24*3600*90);
		return Db::name('order_common')->field($field)->where($where)->select();
	}

	public function getTotalDisByXid($id,$type)
	{
		$where['order_status'] = array('in',array(40,45,50));
		switch ($type){
			case 'user':
				$this->cancelOrder($id);
				$where['uid']= $id;
				break;
			case 'driver':
				$where['driver_id']= $id;
				break;
			default:
				break;
		}
		return Db::name('order_common')->where($where)->sum('real_distance');
	}

	public function getOrderById($id,$field)
	{
		$where['common_id']=$id;
		return Db::name('order_common')->alias('a')->join('__USER__ b','b.uid=a.uid','LEFT')
			->field($field)->where($where)->find();
	}
	/*
	 * order_common表中common_id
	 *
	 * @var string
	 **/
	public function setOrderData($common_id,$data){
		$where['common_id'] = $common_id;
		return Db::name('order_common')->where($where)->update($data);
	}
    /*
     * order_common表中order_sn
     *
     * @var string
     **/
    public function setOrderSnData($order_sn,$data){
        $where['$order_sn'] = $order_sn;
        return Db::name('order_common')->where($where)->update($data);
    }

    public function getOrderList($string='*',$map,$p=1,$limit=10,$order = 'common_id desc')
    {
        return Db::name('order_common')->where($map)->field($string)->order($order)->page($p,$limit)->select();
    }

    public function getForceCancelCount($map)
    {
        return Db::name('user_force_cancel')->where($map)->count();
    }
}